﻿
using Newtonsoft.Json.Linq;
using System;
using System.Xml.Linq;

namespace ExcelCOMAddin
{
    public static class JsonParser
    {
        public static object ParseJson(string json, string field)
        {
            try
            {
                var root = JObject.Parse(json);

                return field switch
                {
                    "last_price" => root["last_price"]?.Value<decimal?>() ?? 0m,
                    "volume" => root["volume"]?.Value<long?>() ?? 0L,
                    "amount" => root["amount"]?.Value<decimal?>() ?? 0m,
                    "open" => root["open"]?.Value<decimal?>() ?? 0m,
                    "close" => root["close"]?.Value<decimal?>() ?? 0m,
                    _ => null
                };
            }
            catch
            {
                return null;
            }
        }
    }
}
